﻿using System;
using System.Threading;
using System.Windows.Forms;
using MessageHandler;

namespace Chatclient
{
	public partial class FormLogin : Form
	{
		Controller controller;
		UserInfo userInfo;
		ErrorHandler errorHandler = new ErrorHandler();

		public Controller Controller
		{
			get { return controller; }
		}

		public FormLogin()
		{
			InitializeComponent();
			controller = new Controller();
			controller.MessageReceived += new Controller.MessageReceivedEventHandler(controller_MessageReceived);
			controller.ErrorOccured += new Controller.ErrorOccuredEventHandler(controller_ErrorOccured);
		}

		private void btnLogin_Click(object sender, EventArgs e)
		{
			try
			{
				this.userInfo = createUserInfo();
				controller.InitConfiguration(userInfo);
				controller.Login(userInfo);
			}
			catch (ChatException err)
			{
				errorHandler.ShowError(err);
			}
		}

		private void btnRegister_Click(object sender, EventArgs e)
		{
			try
			{
				this.userInfo = createUserInfo();
				controller.InitConfiguration(userInfo);
				controller.Register(userInfo);
			}
			catch (ChatException err)
			{
				errorHandler.ShowError(err);
			}
		}

		private UserInfo createUserInfo()
		{
			if (txtBxIP.Text == string.Empty || txtBxName.Text == string.Empty
				|| txtBxPort.Text == string.Empty || txtBxPW.Text == string.Empty)
			{
				throw new ChatException("Bitte alle Felder ausfüllen.");
			}

			UserInfo ui = new UserInfo();
			ui.ip = txtBxIP.Text;
			int port;
			if (int.TryParse(txtBxPort.Text, out port))
			{
				ui.port = port;
			}
			else
			{
				throw new ChatException("Bitte eine gültige Zahl als Port eingeben");
			}
			ui.username = txtBxName.Text;
			ui.password = txtBxPW.Text;
			ui.contacts = new string[0];
			return ui;
		}

		private void controller_MessageReceived(object sender, MessageReceivedEventArgs e)
		{
			if (e.Message.messageType == MessageType.USERLOGIN)
			{
				Login();
			}
			else if (e.Message.messageType == MessageType.REGISTER)
			{
				controller.Login(userInfo);
			}
			else if (e.Message.messageType == MessageType.ERROR)
			{
				errorHandler.ShowError(new ChatException(e.Message.text));
			}
		}

		private void controller_ErrorOccured(object sender, ErrorOccuredEventArgs e)
		{
			errorHandler.ShowError(e.Error);
		}

		private void Login()
		{
			Thread t = new Thread(() => Application.Run(new FormMessengerClient(controller)));
			t.SetApartmentState(ApartmentState.STA);
			t.Start();
			CloseForm();
		}

		private delegate void CloseFormCallback();
		private void CloseForm()
		{
			if (InvokeRequired)
			{
				this.Invoke(new CloseFormCallback(CloseForm));
			}
			else
			{
				this.controller.ErrorOccured -= controller_ErrorOccured;
				this.controller.MessageReceived -= controller_MessageReceived;
				this.Close();
			}
		}

		private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (Application.OpenForms.Count == 0)
			{
				Application.Exit();
			}
		}
	}
}
